/**
 * Created by xiewei on 15/八月/13.
 */
//截取url后缀
var stop = null;
var imgDir = 'img/';
//获取邮编
getRegionCode();
var getRegionTimer = setInterval(function(){
    if(root){
        clearInterval(getRegionTimer);
    }else{
        getRegionCode();
    }
},50);
var shareImg = imgDir+'viptail_logo300.jpg';
var shareTitle = '';
/**
 * 展示寄养故事的日志列表
 */
var storyList = $('#storyList');
var createDiaryList = function(diaryList){
    var results = diaryList;
    for(var j = 0; j < results.length; j++){
        var result = results[j];
        var section = $('<section>').addClass('story-diary').appendTo(storyList);
        //用户信息
        var usrDiv = $('<div>').addClass('story-block');
        if(result.face){
            $('<img>').addClass('img-circle').attr('src',result.face).attr('alt',result.uname).appendTo(usrDiv);
        }else{
            $('<img>').addClass('img-circle').attr('src',imgDir+iconImgs.master).attr('alt',result.uname).appendTo(usrDiv);
        }
        var usrInfoDiv = $('<div>').addClass('right');
        $('<h1>').text(result.uname).appendTo(usrInfoDiv);
        //用户所在地
        var address = $('<p>').appendTo(usrInfoDiv);
        fillAddrInfo(result.uregionid,address);
        usrInfoDiv.appendTo(usrDiv);
        $('<span>').addClass('time').text(timeSettled(result.dateTime)).appendTo(usrDiv);
        //日志内容
        var photoDiv = $('<div>').addClass('pic_list');
        if(result.mediaList.length > 0){
            var medias = result.mediaList;
            shareImg = medias[0].cover+imgSize.thumbnail;
            for(var i = 0; i < medias.length ; i ++){
                var media = medias[i];
                var divW = getPageWidth();
                var divH = divW*media.coverScale;
                var div = $('<div>').addClass('video');
                var imgW = divW*0.2;
                var img = $('<img>').attr('src',imgDir+'button_album_video_play.png')
                    .css({
                        width: '20%',
                        marginLeft: '-60%',
                        marginTop: divH/2-imgW/2
                    });
                var video = $('<video>').attr('src',media.media)
                    .attr('poster',media.cover+imgSize.thumbnail).attr('data',media.id)
                    .attr('preload','auto').addClass('stop')
                    .css({height: divH});
                var p = $('<p>').text('您的浏览器不支持播放该视频:(').appendTo(video);
                video.appendTo(div);
                img.appendTo(div);
                //点击播放\暂停
                div.on('click',function(){
                    if(video.hasClass('stop')){
                        video[0].play();
                        video.removeClass('stop');
                        video.next().addClass('hidden');

                        var videoTimer = setInterval(function(){
                            if(video[0].paused || video[0].ended){
                                video.next().removeClass('hidden');
                                clearInterval(videoTimer);
                            }
                        },1000);
                    }else if(!ifMobile()){
                        video[0].pause();
                        video.addClass('stop');
                        video.next().removeClass('hidden');
                    }
                });
                video.on('click',function(e){
                    $(this)[0].play();
                    e.stopPropagation();
                });
                img.on('click',function(e){
                    $(this).prev()[0].play();
                    e.stopPropagation();
                });
                div.appendTo(photoDiv);
            }
        }
        if(result.photos.length > 0){
            var photos = result.photos;
            $('<img>').addClass('cur_photo').attr('src',photos[0].original+imgSize.small).appendTo(photoDiv);
            shareImg = photos[0].original+imgSize.thumbnail;
            if(photos.length > 1){
                var ul = $('<ul>').addClass('topic_pic_list');
                for(var i = 0; i < photos.length ; i ++){
                    var photo = photos[i];
                    var span = $('<span>').attr('src',photo.original+imgSize.small)
                        .css({//先显示默认图片
                            background: 'url('+photo.original+imgSize.face+') no-repeat center center',
                            backgroundSize: 'cover'
                        });
                    if(i==0){span.addClass('selected')}
                    $('<li>').append(span).appendTo(ul);
                }
                var scroller = $('<div>').attr('id','scroller_'+j).addClass('scroller')
                    .css({ width: 75*photos.length });//所有图片总长 图片数*单张图片所占宽度
                var wrapper = $('<div>').attr('id','wrapper_'+j).addClass('wrapper');
                wrapper.append(scroller.append(ul)).appendTo(photoDiv);
            }
        }
        $('<pre>').addClass('emoji_p').text(result.content).appendTo(photoDiv);
        //注脚信息
        var bottomDiv = $('<div>').addClass('diary-remark');
        $('<p>').text(result.address).appendTo(bottomDiv);//地址
        //频道
        var p = $('<p>').addClass('channel').appendTo(bottomDiv);
        if(result.channelList && result.channelList.length>0){
            var channel = result.channelList[0];
            $('<img>').attr('src',channel.icon).appendTo(p);
            $('<label>').text(channel.title).appendTo(p);
        }
        //收藏、点赞、评论数
        var countDiv = $('<div>').addClass('right').appendTo(bottomDiv);
        var collect = result.collectCount?result.collectCount:0;
        $('<span>').addClass('icon-label')
            .html('<span class="icon-10 icon-collect"></span><label>'+collect+'</label>').appendTo(countDiv);
        var comment = result.commentCount?result.commentCount:0;
        $('<span>').addClass('icon-label')
            .html('<span class="icon-10 icon-comment"></span><label>'+comment+'</label>').appendTo(countDiv);
        var praise = result.praiseCount?result.praiseCount:0;
        $('<span>').addClass('icon-label')
            .html('<span class="icon-10 icon-praise"></span><label>'+praise+'</label>').appendTo(countDiv);

        section.append(usrDiv,photoDiv,bottomDiv);
        //多张图片
        if(result.photos.length > 1){
            //bottomDiv.addClass('mt80');
            //图片列表水平滚动
            new IScroll('#wrapper_'+j, { scrollX: true, scrollY: false, mouseWheel: true, click: true });
        }
        //表情转换
        emojifyTo(section.find('pre.emoji_p'));
    }
    setShareInfo(shareTitle,results[0]);
    //图片点击事件 查看大图
    $(".topic_pic_list").each(function(){
        var curPhoto = $(this).parent().parent().prev();
        var picList = $(this);
        $(this).find('span').on('click',function(){
            curPhoto.attr('src',$(this).attr('src'));
            picList.find('.selected').removeClass('selected');
            $(this).addClass('selected');
        })
    });
}
loading();
var storyId = getUrlParam('storyId');
//获取用户信息
var storyReqData = {
    storyId: storyId
}
$.ajax({
    url:'/dog2/appSocial/getStroyInfo',
    data: paramCoding(storyReqData),
    type: 'POST',
    dataType: 'json',
    success: function(res){
        if(res.respCode==0){
            $('#storyHeader').removeClass('hidden');
            //封面
            if(res.coverPhoto && res.coverPhoto.length>0){
                $('#storyBack').css({
                    background: '#000 url("'+res.coverPhoto+'") center center',
                    '-webkit-background-size': 'cover',
                    'background-size': 'cover'
                });
            }
            //宠物
            if(res.petList && res.petList.length>0){
                var ul = $('#petScroller');
                shareTitle = '分享了'+res.petList[0].name+'的寄养故事';
                for(var i=0;i < res.petList.length; i++){
                    var pet = res.petList[i];
                    var li = $('<li>');
                    $('<img>').addClass('img-circle').attr('src',pet.face).appendTo(li);
                    if(pet.sex==1){
                        $('<img>').addClass('sex').attr('src',imgDir+'icon_pet_boy.png').appendTo(li);
                    }else if(pet.sex==0){
                        $('<img>').addClass('sex').attr('src',imgDir+'icon_pet_girl.png').appendTo(li);
                    }
                    ul.append(li);
                }
                //$('#petScroller').css({width: 60*res.petList.length});
                //new IScroll('#petWrapper',{ mouseWheel: true });
            }
            //故事信息
            $('#storyTitle').text(res.title);
            $('#storyStart').text(res.startTime.replace(/-/g,'.'));
            $('#storyEnd').text(res.endTime.replace(/-/g,'.'));
            $('#storyFamily').text(res.ffname);
            $('#storyMaster').text(res.uname);
            //寄养日志
            if(res.dairyList && res.dairyList.length>0){
                createDiaryList(res.dairyList);
            }else{
                setShareInfo(shareTitle,shareImg);
            }
            $('#bottomImg').removeClass('hidden');
        }
        loaded();
    },
    error: function(){
        showMessageAlert('粗错了');
        loaded();
    }

})
